
import requests
from lxml import  etree

if __name__ == '__main__':
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
                      ' Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.76'
    }
    fp = open('58.txt', 'w', encoding='utf-8')
    i = int(0)
    for j in range(1,2):#查询的页数，从1开始，过多可能会被限制访问
        url = 'https://bj.58.com/ershoufang/pj'

        page_text = requests.get(url = url,headers=headers).text

        tree = etree.HTML(page_text)
        # page_text_tile = tree.xpath('//div[@class = "property-content"]//h3/@title')
        # print(page_text_tile)

        div_list = tree.xpath('//section[@class = "list"]/div')
        for div in div_list:
            div_h3_text = div.xpath('.//div[@class = "property-content-title"]//text()')
            div_price = div.xpath('.//span[@class = "property-price-total-num"]/text()')
            div_price1m = div.xpath('.//div[@class = "property-price"]/p[2]/text()')
            div_digsmall = div.xpath('.//div[@class = "property-content-info"]/p[2]/text()')


            # div_d=big = tree.xpath('.//p[2]/text()')
            i+= 1
            print(div_h3_text,div_price,div_digsmall,div_price1m,i)

            fp.write('第'+'%d'%i+'条、'+str(div_h3_text)+'-----房屋总价：'+str(div_price)+'万'+'-----每平米单价：'
                     +str(div_price1m)+'-----总面积：'+str(div_digsmall)+'\n')


